package com.zjkj.mp.controller.notice;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.zjkj.mp.service.notice.NoticeManager;
import com.zjkj.mp.utils.PaginatedResult;
import com.zjkj.mp.vo.notice.Notice;

/**
 * 管理系统公告
 * @author wxd
 *
 */
@Controller
public class NoticeController {
	private static final int PAGE_SIZE = 12;
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@Autowired
	NoticeManager noticeManager;
	
	/**
	 * 显示新建系统公告的页面
	 * @return
	 */
	@RequestMapping("/newNotice.do")
	public String newNotice(){
		return "notice/newNotice";
	}
	
	/**
	 * 保存新建的公告
	 */
	@RequestMapping("/doNewNotice.do")
	public String doNewNotice(HttpServletRequest req){
		String title = req.getParameter("title");
		String content = req.getParameter("content");
		String visible = req.getParameter("visible");
		
		Notice notice = new Notice();
		notice.setContent(content);
		notice.setTitle(title);
		notice.setVisible(visible.equals("1")?true:false);
		notice.setCreateDate(sdf.format(new Date()));
		
		this.noticeManager.createNotice(notice);		
		return adminNoticeList(req);		
	} 
	
	/**
	 * 得到管理系统公告的列表
	 * @param req
	 * @return
	 */
	@RequestMapping("/adminNoticeList.do")
	public String adminNoticeList(HttpServletRequest req){
		String pageNoStr = req.getParameter("pageNo");
		int pageNo = 1;
		if(pageNoStr != null){
			pageNo = Integer.parseInt(pageNoStr);
		}
		
		PaginatedResult result = this.noticeManager.getList(pageNo, PAGE_SIZE, null);
		req.setAttribute("result", result);
		
		return "notice/adminNoticeList";
	} 
	
	/**
	 * 隐藏公告
	 * @return
	 */
	@RequestMapping("/hideNotice.do")
	public String hideNotice(HttpServletRequest req){
		String id = req.getParameter("id");
		String visible = req.getParameter("visible");
		Notice notice = this.noticeManager.getNoticeById(Integer.parseInt(id));
		notice.setVisible("true".equals(visible)?true:false);
		this.noticeManager.updateNotice(notice);
		
		return adminNoticeList(req);
	}
	
	/**
	 * 显示编辑界面
	 */
	@RequestMapping("/editNotice.do")
	public String editNotice(@RequestParam("id") int id,HttpServletRequest req){
		Notice notice = this.noticeManager.getNoticeById(id);
		req.setAttribute("notice", notice);
		
		return "notice/editNotice";
	}
	
	/**
	 * 保存编辑后的公告内容
	 */
	@RequestMapping("/doEditNotice.do")
	public String doEditNotice(HttpServletRequest req){
		String title = req.getParameter("title");
		String content = req.getParameter("content");
		String visible = req.getParameter("visible");
		String id = req.getParameter("id");
		
		Notice notice = this.noticeManager.getNoticeById(Integer.parseInt(id));
		if(notice != null){
			notice.setContent(content);
			notice.setTitle(title);
			notice.setVisible(visible.equals("1")?true:false);
			this.noticeManager.updateNotice(notice);
			req.setAttribute("msg","修改成功！");
			return this.editNotice(Integer.parseInt(id), req);
		}
		return null;	
	}
	
	/**
	 * 删除公告
	 */
	@RequestMapping("/deleteNotice.do")
	public String deleteNotice(HttpServletRequest req){
		String id = req.getParameter("id");
		this.noticeManager.deleteNotice(Integer.parseInt(id));
		return this.adminNoticeList(req);
	}
}
